const strFormat = require("../../util/strFormat");
const sql = data => {
  return strFormat(
    "delete from businline " + 
    "where licence='{licence}'; " + 
    "insert into businline " + 
    "(licence, lineid) " + 
    "values ('{licence}', {lineid})",
    data
  );
};

const modifybusinline = (connection, req, res) => {
  connection.query(sql(req.body.data), (err, result) => {
    var data = null;

    if ( err || result.affectedRows === 0 ) {
      if ( err ) console.log(err);
      res.status(200).json({
        status: "fail",
        data: data,
      });
      return;
    } else {
      data = {
        licence: req.body.data.licence,
        lineid: req.body.data.lineid,
      };

      res.status(200).json({
        status: "success",
        data: data,
      });
    }
  });
};

module.exports = modifybusinline;